const router = require('koa-router')()
const cc = require('../conCtl')
const js = require('../jsCtl')
const db = require('../dbCtl')


/**
@hostpath = http://127.0.0.1/client/users
 */

/** 获取指定用户信息
###
    GET {{hostpath}}/oM2uY56Bz-UCxSNWumQTUA5cq3og
### */
router.get('/:openid', async c => {
    //获取用户信息
    let data = await db.model('t_users')
        .where('openid=?', c.params.openid)
        .field('openid,nick,headurl,points,lottery_ticket,tel')
        .shift('未找到用户');
    //获取邀请奖品信息
    data.invitation = await db.model('t_invitation').shift('未找到指定邀请奖品');
    //获取当前用户的订单信息
    data.order = await db.model('t_order a')
        .join('t_goods b on a.goods_id=b.id')
        .join('t_wuliu_company c on a.wuliuc_id=c.id')
        .join('t_wuliu d on a.wuliu_id=d.id')
        .field('a.order_id,b.name,b.details,a.state,a.type,a.dt,c.name as wuliu_name,a.wuliu_id,d.no,a.delivery_time,a.goods_id,d.address,a.address_id')
        .where('a.openid=?', c.params.openid)
        .orderBy('a.dt desc')
        .select();
    for (let i = 0; i < data.order.length; i++) {
        const element = data.order[i];
        element.details = element.details.split(';')[0];
        if (!js.isNullOrUndefined(element.address_id)) {
            let add = await db.model('t_user_address').where('address_id=?', element.address_id).shift();
            element.user_address = add.province + add.city + add.area + add.address;
        }
    }
    //获取当前用户参加的正在拍卖信息
    data.having_auction = await db.model('t_auction_log a')
        .join('t_auction_goods b on a.auction_goods_id=b.auction_goods_id')
        .join('t_auction c on b.auction_id=c.id')
        .where('a.offer_type=1&&a.offer_openid=?', c.params.openid)
        .field('c.*')
        .groupBy('c.id')
        .select();
    // cc.log(data.having_auction);
    cc.send(c, data);
});

/** 更新用户资料
###
    POST {{hostpath}}/update_users
    Content-Type: application/json

    {
        "openid":"test_openid",
        "tel":"1369090980"
    }
### */
router.post('/update_users', async c => {
    js.check_params(!c.request.body.tel && '!电话号码')

    let openid = c.request.body.openid;
    let data = { tel: c.request.body.tel };
    await db.model('t_users').where('openid=?', openid).relyUpdate('修改失败', data);

    cc.send(c);
})

/** 确认收货地址
###
    POST {{hostpath}}/confirm_address
    Content-Type: application/json

    {
        "order_id":"2",
        "address_id":"1"
    }
### */
router.post('/confirm_address', async c => {
    js.check_params(!c.request.body.order_id && '!订单信息')
    js.check_params(!c.request.body.address_id && '!用户地址')

    await db.model('t_order').where('order_id=?', c.request.body.order_id).update({ address_id: c.request.body.address_id, state: 3 });
    cc.send(c);
})

/** 我的全部订单
###
    POST {{hostpath}}/order_all
    Content-Type: application/json

    {
        "openid":"oM2uY59aMmztLxDeqN2hC8fcCo8c",
        "page":1,
        "size":30
    }
### */
router.post('/order_all', async c => {
    let
        page = c.request.body.page || 1,
        size = c.request.body.size || 30;
    js.check_params(!c.request.body.openid && '!用户信息')
    let data = await db.model('t_order a')
        .join('t_goods b on a.goods_id=b.id')
        .join('t_wuliu_company c on a.wuliuc_id=c.id')
        .join('t_wuliu d on a.wuliu_id=d.id')
        .field('a.order_id,b.name,b.details,a.state,a.type,a.dt,c.name as wuliu_name,a.wuliu_id,d.no,a.delivery_time,a.goods_id,d.address,a.address_id')
        .where('a.openid=?', c.request.body.openid)
        .orderBy('a.dt desc')
        .select(page, size);
    let count = await db.model('t_order').where('openid=?', c.request.body.openid).count();

    cc.send_list(c, data, count);
})

/** 订单详情
###
    POST {{hostpath}}/order_detail
    Content-Type: application/json

    {
        "order_id":1
    }
### */
router.post('/order_detail', async c => {
    js.check_params(!c.request.body.order_id && '!订单信息')

    let data = await db.model('t_order a').join('t_goods b on a.goods_id=b.id')
        .where('a.order_id=?', c.request.body.order_id)
        .field('b.details,b.name,a.state,a.type,a.dt,a.money,a.order_id')
        .shift();
    data.details = data.details.split(';')[0];
    cc.send(c, data);
})

/** 确认收货
###
    POST {{hostpath}}/order_confirm
    Content-Type: application/json

    {
        "order_id":14,
        "openid":"oM2uY59aMmztLxDeqN2hC8fcCo8c"
    }
### */
router.post('/order_confirm', async c => {
    js.check_params(!c.request.body.order_id && '!订单信息' ||
        !c.request.body.openid && '!用户信息')

    await db.model('t_order').where('order_id=?&&openid=?', c.request.body.order_id, c.request.body.openid).update({ state: 5 });
    cc.send(c);
})



/** 积分充值数据
###
    POST {{hostpath}}/score
### */
router.post('/score', async c => {
    let data = await db.model('t_money_set').select();
    cc.send(c, data);
})


//





module.exports = router